Skip to content

Offer rendered height and width on text objects - #425

Open
petero-dk wants to merge 6 commits into
modesty:masterfrom
zealofzebras:renderedheight
Open

Offer rendered height and width on text objects#425
petero-dk wants to merge 6 commits into
modesty:masterfrom
zealofzebras:renderedheight

Conversation

@petero-dk

Copy link
Copy Markdown

This pull request introduces improvements to the PDF text rendering pipeline, ensuring more accurate vertical text metrics and enhancing the output data structure. The main changes involve passing and calculating the rendered text height (renderedHeight) throughout the rendering and data extraction layers, updating method signatures, and reflecting these changes in the output and documentation. Additionally, a new test script is added for verifying hidden field extraction.

Rendering and Text Metrics Improvements:

  • Updated the CanvasGraphics rendering logic in base/display/canvas.js to calculate and pass renderedHeight when rendering both Type3 and regular fonts, and to compute vertical and horizontal scaling factors for more accurate text box metrics. [1] [2] [3]
  • Modified the CanvasRenderingContext2D_ class in lib/pdfcanvas.js to accept and propagate the new renderedHeight parameter in fillText and strokeText methods. [1] [2]
  • Enhanced the PDFFont class in lib/pdffont.js to receive renderedHeight, calculate horizontal scaling, and output both rendered width (rw) and rendered height (rh) in the text block data. [1] [2]

Output Data Structure and Documentation:

  • Updated the documentation in readme.md to describe the new h (rendered text box height) field in the Texts array, clarifying its meaning and independence from horizontal metrics.

Testing:

  • Added a new test script test/test-hidden-field.cjs to verify extraction and display of text, field, and boxset metrics, including the new rendered width and height fields.

petero-dk and others added 6 commits July 21, 2026 18:04
… units

rw and rh were computed in raw PDF text-space units rather than viewport
pixels, making them ~1.5x too small relative to x/y coordinates.

In showText(), canvasWidth, curFontSize, and renderedHeight are all
accumulated in PDF text-space (before the viewport transform). x/y come
from getCoords_(0,0) which applies the full cumulative canvas matrix
(viewport × text matrix), so they ARE in viewport pixels.

Fix: multiply the PDF-space components by the viewport scale factor
(derived from this.baseTransform) before passing to fillText, so that
rw and rh end up in the same coordinate system as x and y.

Result for a 20pt×20pt box at (20pt, 20pt):
  Before: x=1, y=1.199, rw=0.799, rh=0.737
  After:  x=1, y=1.199, rw=1.198, rh=1.043  (rw≈y, rh≈x as expected)

Applied to both the regular-font and Type3-font rendering paths.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

@modesty modesty left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changes to lib/pdfcanvas.js should comply with W3C/WHATWG Canvas API contract. See inline comments for more details.

The preferred way is to use a private extraction helper or context state instead of extending the public Canvas methods.

Comment thread lib/pdfcanvas.js
}

strokeText(text, x, y, maxWidth) {
strokeText(text, x, y, maxWidth, fontSize, renderedHeight) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fillText and strokeText violate the Canvas API contract.

The W3C/WHATWG Canvas contract defines only text, x, y, optional maxWidth; fontSize and renderedHeight
have no API meaning. The current WHATWG Canvas specification
(https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-filltext) and W3C 2D Context reference
(https://www.w3.org/2015/06/2dcontext.html) agree. Extra JavaScript arguments may be passed, but the
standard operation does not consume them; Web IDL
(https://webidl.spec.whatwg.org/#overload-resolution-algorithm) ignores trailing arguments beyond the
declared overload.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants